package juc.c_020;
/*
   Talk is cheap. Show me the code.
 */

import java.util.concurrent.Semaphore;

/**
 * @Description 信号量。允许多少线程并发，常用于限流
 * @Date 2021/6/20
 * @Author liudl
 */
public class T11_Semaphore {
//    static Semaphore semaphore = new Semaphore(1);
    static Semaphore semaphore = new Semaphore(1,true);

    public static void main(String[] args) {
        threadStart();
        threadStart();
    }

    private static void threadStart() {
        new Thread(()->{
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName()+" start.");
                Thread.sleep(200);
                System.out.println(Thread.currentThread().getName()+" running.");
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                semaphore.release();
            }
        }).start();
    }
}
